package com.atguigu.crowd.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.jdom.adapters.CrimsonDOMAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.entity.AdminExample;
import com.atguigu.crowd.entity.AdminExample.Criteria;
import com.atguigu.crowd.exception.LoginAcctAlreadyInUseException;
import com.atguigu.crowd.exception.LoginAcctAlreadyInUseForUpdateException;
import com.atguigu.crowd.exception.LoginFailedException;
import com.atguigu.crowd.mapper.AdminMapper;
import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.util.CrowdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class AdminServiceImpl implements AdminService {

	@Autowired
	private AdminMapper adminMapper;
	
	@Autowired
	private BCryptPasswordEncoder passwordEncoder;
	
	@Override
	public void saveAdmin(Admin admin) {
		String userPswd = admin.getUserPswd();
		//userPswd = CrowdUtil.md5(userPswd);
		userPswd = passwordEncoder.encode(userPswd);
		admin.setUserPswd(userPswd);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = sdf.format(date);
		admin.setCreateTime(format);
		try {
			
			adminMapper.insert(admin);
		} catch (Exception e) {
			e.printStackTrace();
			if (e instanceof DuplicateKeyException) {
				
				throw new LoginAcctAlreadyInUseException(CrowdConstant.MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQUE);
			}
		}
	}

	@Override
	public List<Admin> getAll() {
		return adminMapper.selectByExample(new AdminExample());
	}

	@Override
	public Admin getAdminByLoginAcctAndUserPwd(String loginAcct, String userPswd) {
		AdminExample adminExample = new AdminExample();
		Criteria criteria = adminExample.createCriteria();
		criteria.andLoginAcctEqualTo(loginAcct);
		criteria.andUserPswdEqualTo(CrowdUtil.md5(userPswd));
		List<Admin> adminList = adminMapper.selectByExample(adminExample);
		if (adminList==null || adminList.size()==0) {
			throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
		}
		if (adminList.size()>1) {
			throw new LoginFailedException(CrowdConstant.MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQUE);
		}
		Admin admin = adminList.get(0);
		if (admin == null) {
			throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
		}
		return admin;
	}

	@Override
	public PageInfo<Admin> getPageInfo(Integer pageNum, Integer pageSize, String keyword) {
		PageHelper.startPage(pageNum, pageSize);
		List<Admin> list = adminMapper.selectAdminByKeyword(keyword);
		PageInfo<Admin> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	public Admin getAdminById(Integer adminId) {
		return adminMapper.selectByPrimaryKey(adminId);
	}

	@Override
	public void update(Admin admin) {
		try {
			adminMapper.updateByPrimaryKeySelective(admin);
		} catch (Exception e) {
			e.printStackTrace();
			if (e instanceof DuplicateKeyException) {
				
				throw new LoginAcctAlreadyInUseForUpdateException(CrowdConstant.MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQUE);
			}
		}
	}

	@Override
	public void remove(Integer adminId) {
		adminMapper.deleteByPrimaryKey(adminId);
	}

	@Override
	public void saveAdminRoleRelatinship(Integer adminId, List<Integer> roleIdList) {
		//删除原先的role
		adminMapper.deleteOldRelationship(adminId);
		//保存现在的role
		if (roleIdList!=null&&roleIdList.size()>0) {
			
			adminMapper.insertNewRelationship(adminId,roleIdList);
		}
	}

	@Override
	public Admin getAdminByLoginAcct(String loginAcct) {
		AdminExample adminExample = new AdminExample();
		adminExample.createCriteria().andLoginAcctEqualTo(loginAcct);
		List<Admin> adminList = adminMapper.selectByExample(adminExample);
		Admin admin = adminList.get(0);
		return admin;
	}

	@Override
	public void batchRemove(List<Integer> ids) {
		AdminExample adminExample = new AdminExample();
		Criteria createCriteria = adminExample.createCriteria();
		createCriteria.andIdIn(ids);
		adminMapper.deleteByExample(adminExample);
	}

}
